﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class Level: IEquatable<Level>
    {
        public void ImportList(List<Classes.Level> levelList)
        {
            var mdb = new MedregDBDataContext();
            var ListLevel = new List<Level>();
            foreach (var level in levelList)
            {
                Level level1 = new Level { ID = level.ID, Name = level.Name, Parent = level.Parent };
                Level query;

                if (mdb.Level.LongCount() == 0)
                {
                    mdb.Level.InsertOnSubmit(level1);
                }
                else
                {
                    try
                    {
                        query = mdb.Level.Single(l => l.ID == level1.ID);
                        query.Name = level1.Name;
                        query.Parent = level1.Parent;
                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {
                        
                        if (!ListLevel.Contains(level1))
                        {
                            mdb.Level.InsertOnSubmit(level1);
                            ListLevel.Add(level1);
                        }
                        
                    }
                }
            }
            mdb.SubmitChanges();
        }
        public bool Equals(Level other)
        {
            var i = this.ID.Equals(other.ID);
            var j = this.Name.Equals(other.Name);
            if (i & j)
            {
                return true;
            }
            return false;
        }
    }
}
